<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { modifyPassFormSchema } from './useLogin';
  import { initpwdcheck, changeloginpassword } from '/@/api/system/system';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { encryptByRsa } from '/@/utils/cipher';

  export default defineComponent({
    name: 'UserLoginEditPwdModal',
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const { createMessage } = useMessage();
      const title = ref();

      const [registerForm, { resetFields, validate, setFieldsValue, updateSchema }] = useForm({
        labelWidth: 100,
        schemas: modifyPassFormSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 23,
        },
      });

      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        title.value = data.title;
        resetFields();
        setModalProps({ confirmLoading: false, loading: true, maskClosable: false });
        setFieldsValue({
          userName: data.userName,
        });
        const passwordVeriry = await initpwdcheck();
        updateSchema({
          field: 'password',
          dynamicRules: () => {
            return [
              {
                required: true,
                // @ts-ignore
                validator: async (rule, value) => {
                  if (!value) {
                    /* eslint-disable-next-line */
                    return Promise.reject('请输入密码');
                  }
                  if (
                    value.length < passwordVeriry.minLen ||
                    value.length > passwordVeriry.maxLen
                  ) {
                    return Promise.reject(
                      `必须是${passwordVeriry.minLen}-${passwordVeriry.maxLen}位的字符`
                    );
                  }
                  return Promise.resolve();
                },
                trigger: 'change',
              },
            ];
          },
        });
        setModalProps({ confirmLoading: false, loading: false });
      });

      async function handleSubmit() {
        try {
          const values = await validate();
          setModalProps({ confirmLoading: true });
          values.oldPassword = encryptByRsa(values.oldPassword);
          values.password = encryptByRsa(values.password);
          values.confirmPassword = encryptByRsa(values.confirmPassword);
          await changeloginpassword(values);
          createMessage.success('操作成功，请使用新密码登录');
          closeModal();
          emit('success');
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return { registerModal, registerForm, handleSubmit, title };
    },
  });
</script>
